package com.gdth.bank.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.model.ImageItem;
import com.gdth.utils.ToolUtil;
import com.gdth.widget.listview.adapter.ImageSwipeAdapter;
import com.gdth.widget.listview.swipe.SwipeMenuListView;
import com.gdth.widget.listview.swipe.listener.OnMenuItemClickListener;
import com.gdth.widget.listview.swipe.listener.OnSwipeMenuListener;
import com.gdth.widget.listview.swipe.menu.SwipeMenu;
import com.gdth.widget.listview.swipe.menu.SwipeMenuItem;
import com.gdth.widget.pulltorefresh.library.PullToRefreshBase;
import com.gdth.widget.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.gdth.widget.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.gdth.widget.pulltorefresh.library.PullToRefreshBase.State;
import com.gdth.widget.pulltorefresh.library.PullToRefreshSwipwListView;
import com.gdth.widget.pulltorefresh.library.extras.SoundPullEventListener;
import com.gdth.bank.R;
import com.gdth.bank.presenter.MediaPresenter;
import com.gdth.bank.view.MediaView;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;

public class MediaListActivity extends BaseActivity implements MediaView{
	
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right) TextView txt_header_right;
	
	@ViewInject(id=R.id.listView) PullToRefreshSwipwListView RefreshlistView;
	
	SwipeMenuListView listView;
	
	private ImageSwipeAdapter mAdapter;
	
	List<ImageItem> listData;
	
	MediaPresenter mMediaPresenter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_media_list);
		
		mMediaPresenter = new MediaPresenter(this);
		initView();
	}

	private void initView() {
		txt_header_title.setText(R.string.title_activity_media_list);
		txt_header_right.setVisibility(View.GONE);
        
		RefreshlistView = (PullToRefreshSwipwListView) findViewById(R.id.listView);
		listView = RefreshlistView.getRefreshableView();
		
		RefreshlistView.setOnRefreshListener(new OnRefreshListener<SwipeMenuListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
				String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				
				updateView(0);
			}
		});

		RefreshlistView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

			@Override
			public void onLastItemVisible() {
				Toast.makeText(MediaListActivity.this, "已经到底了", Toast.LENGTH_SHORT).show();
			}
		});

		registerForContextMenu(listView);
        
		SoundPullEventListener<SwipeMenuListView> soundListener = new SoundPullEventListener<SwipeMenuListView>(this);
		soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
		soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
		soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
		RefreshlistView.setOnPullEventListener(soundListener);
   
		listData = new ArrayList<ImageItem>();
		mAdapter = new ImageSwipeAdapter(this, listData);

		RefreshlistView.setAdapter(mAdapter);

		// step 1. create a MenuCreator
		OnSwipeMenuListener creator = new OnSwipeMenuListener() {

			@Override
			public void create(SwipeMenu menu) {
				// Create different menus depending on the view type
				createMenu(menu);
			}

			private void createMenu(SwipeMenu menu) {
				SwipeMenuItem item1 = new SwipeMenuItem(getApplicationContext());
				item1.setBackground(new ColorDrawable(Color.rgb(0xE5, 0x18, 0x5E)));
				item1.setWidth(listView.dp2px(90));
				item1.setIcon(R.drawable.ic_action_share);
				menu.addMenuItem(item1);
				
				SwipeMenuItem item2 = new SwipeMenuItem(getApplicationContext());
				item2.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
				item2.setWidth(listView.dp2px(90));
				item2.setIcon(R.drawable.ic_action_discard);
				menu.addMenuItem(item2);
			}
		};
		
		listView.setMenuCreator(creator);

		listView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				ImageItem item = listData.get(position);
				switch (index) {
					case 0:
						play(position);
						break;
					case 1:
						// delete
	//					delete(position);
						ToolUtil.deleteFile(item.getValue());
						listData.remove(position);
						mAdapter.notifyDataSetChanged();
						break;
				}
			}
		});
		
		updateView(0);
	}
	
	@Override
   	public boolean onCreateOptionsMenu(Menu menu) {
   		menu.add(0, 0, 0, "操作1");
   		menu.add(0, 1, 0, "操作2");
   		return super.onCreateOptionsMenu(menu);
   	}
       
   	@Override
   	public boolean onOptionsItemSelected(MenuItem item) {
   		switch (item.getItemId()) {
   			case 0:
   				break;
   			case 1:
   				break;
   		}

   		return super.onOptionsItemSelected(item);
   	}

   	@Override
   	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   		menu.setHeaderTitle("这是菜单: ");// + getListView().getItemAtPosition(info.position)
   		menu.add("菜单1");
   		menu.add("菜单2");

   		super.onCreateContextMenu(menu, v, menuInfo);
   	}

   	@Override
   	public boolean onPrepareOptionsMenu(Menu menu) {
   		return super.onPrepareOptionsMenu(menu);
   	}
	
	public void updateView(int what) {
		mMediaPresenter.getAll();
		List<ImageItem> result = new ArrayList<ImageItem>();
		Vector<String> mList = ToolUtil.GetVideoFileName();
		int count = 0;
		for (String obj : mList){
			ImageItem mImageItem = new ImageItem();
			mImageItem.setValue(obj);
			mImageItem.setKey("item"+ count);
			
			result.add(mImageItem);
			
			count += 1;
		}
		switch (what) {
			case 0:
				listData.clear();
				listData.addAll(result);
				break;
			case 1:
				listData.addAll(result);
				break;
		}

		RefreshlistView.onRefreshComplete();
		mAdapter.notifyDataSetChanged();
	}

	private void play(int arg0){
		ImageItem item = listData.get(arg0);
		
		Intent intent = new Intent(this, VideoActivity.class);
		intent.putExtra("path", item.getValue());
		this.startActivity(intent);
	}

	@Override
	public void updateView(int type, Object arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showProgressDialog() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showError(String msg) {
		// TODO Auto-generated method stub
		
	}
}
